package main

import (
	"fmt"
	"github.com/antchfx/htmlquery"
	"io"
	"net/http"
	"strings"
)

func main() {
	var client = http.Client{}
	var result []string
	baseURL := "https://movie.douban.com/top250?start=%d&filter="

	for start := 0; start < 250; start += 25 {
		url := fmt.Sprintf(baseURL, start)
		req, err := http.NewRequest("GET", url, nil)
		if err != nil {
			panic(err)
		}

		req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")

		resp, err := client.Do(req)
		if err != nil {
			panic(err)
		}
		defer func(Body io.ReadCloser) {
			err := Body.Close()
			if err != nil {
				panic(err)
			}
		}(resp.Body)

		doc, err := htmlquery.Parse(resp.Body)
		if err != nil {
			panic(err)
		}

		ols := htmlquery.Find(doc, "//*[@id=\"content\"]/div/div[1]/ol/li")
		for _, ol := range ols {
			name := htmlquery.FindOne(ol, "//div/div[2]/div[2]/p[1]")
			text := htmlquery.InnerText(name)
			text1 := strings.TrimSpace(text)

			sentences := splitTextIntoSentences(text1)
			result = append(result, sentences...)
			fmt.Println(result)
		}
	}
}

func splitTextIntoSentences(text string) []string {
	// 切割文本为多个句子
	var sentences []string
	var currentSentence string
	insideQuotes := false

	for _, char := range text {
		if char == '"' {
			insideQuotes = !insideQuotes
		}

		if char == ' ' && !insideQuotes {
			// 遇到空格且不在引号内，将当前句子添加到结果
			sentences = append(sentences, currentSentence)
			currentSentence = ""
		} else {
			currentSentence += string(char)
		}
	}

	// 添加最后一个句子，包括余下的文本
	sentences = append(sentences, currentSentence)

	return sentences
}
